// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    // 默认数据
    data: {
        warehouseOption: [],
        now: '',
        state: [],
        result: [],
        page: 1,
        pageAll: 0,
        where: '',
        sortShow: false,
        sortArray: [],
        searchShow: false,
        searchArray: {}
    },
    // 获取仓库列表
    search: async function () {
        let result = await api.curlGet('?cla=machiningOrder&fun=search')
        if (result.code === 0) {
            api.showToast(res.warn)
        } else {
            const res = result.data
            this.setData({
                sortArray: res.orderBy,
                searchArray: res.search,
                state: res.state
            })
        }
    },
    // 列表数据
    list: async function (param = {}, page = 1) {
        const url = '?cla=machiningOrder&fun=list&page=' + page
        let res = await api.curlPost(url, param)
        if (res.code === 1) {
            const page = res.data.page
            this.setData({
                result: res.data.result,
                page: page.pageNow,
                pageAll: page.pageAll,
                where: api.arrayToJson(param)
            })
        } else {
            api.showToast(res.warn)
        }
    },
    onSearch: async function (e) {
        const orderNo = e.detail
        if (orderNo) {
            const state = wx.getStorageSync('state') ? wx.getStorageSync('state') : '待入库'
            this.list({
                orderNo: orderNo,
                state: state
            })
        }
    },
    onChangeState: async function (e) {
        let state = e.detail.name
        if (state) {
            wx.setStorageSync('nowState', state)
            this.list({state: state})
        }
    },
    onScan: async function (e) {
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                if (res.errMsg === "scanCode:ok") {
                    const string = res.result
                    let array = api.parseStr(string)
                    if (['machiningOrderOther', 'machiningOrder'].includes(array['source'])) {
                        wx.navigateTo({
                            url: 'detailOther?id=' + array['id'],
                        })
                    }
                }
            }
        })
    },
    onDetail: function (obj) {
        let data = obj.currentTarget.dataset
        if (data.id) {
            wx.navigateTo({
                url: 'detail?id=' + data.id,
            })
        }
    },
    main: async function () {
        await this.search()
        await this.list()
    },
    onLoad: async function (options) {
        this.main()
    },
    addInStock: function (options) {
        wx.navigateTo({
            url: 'add',
        })
    },
    // 上拉加载
    onReachBottom() {
        if (!this.loading && this.data.page < this.data.pageAll) {
            const page = parseInt(this.data.page),
                param = api.jsonToArray(this.data.where)
            this.list(param, page + 1)
        }
    },
    // 下拉刷新
    onPullDownRefresh() {
        if (!this.loading) {
            const param = api.jsonToArray(this.data.where)
            this.list(param).then(() => {
                // 处理完成后，终止下拉刷新
                wx.stopPullDownRefresh()
            })
        }
    },
    showHide(obj, bool = true) {
        const field = obj.currentTarget.dataset.target
        if (field) {
            this.setData({
                [field]: bool
            })
        }
    },
    // 排序
    sortFun(obj) {
        let array = obj.detail,
            that = this
        if (array.act === '确定') {
            that.list({
                orderBy: array.val
            })
        }
        that.setData({
            sortShow: false
        })
    },
    // 筛选
    searchFun(obj) {
        let array = obj.detail,
            that = this
        if (array.act === '确定') {
            that.list(array.param)
        }
        that.setData({
            searchShow: false
        })
    }
});